<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-logo">
        <img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt="" />
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login-btn">
          <el-button type="primary" @click="onLogin">登录</el-button>
          <el-button type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名' },
          {
            min: 3,
            max: 10,
            message: '长度需要在3到10之间',
            trigger: ['blur'],
          },
        ],
        password: [
          { required: true, message: '请输入密码' },
          {
            pattern: /^\w{6,15}$/,
            message: '长度需要在6-15只见',
            trigger: ['blur'],
          },
        ],
      },
    }
  },
  methods: {
    // 登录
    async onLogin() {
      try {
        await this.$refs.form.validate()
        const res = await login(this.form)
        setToken(res.data.token)
        this.$store.commit('user/GETUSER', res.data.token)
        this.$router.push('/')
        // console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #2b4b6b;
  .login-box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    .login-logo {
      width: 130px;
      height: 130px;
      margin: 0 auto;
      padding: 5px;
      border-radius: 50%;
      border: 1px solid #eee;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      transform: translateY(-50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .el-form {
      position: absolute;
      bottom: 0;
      padding: 0 20px;
      width: 410px;
      .login-btn {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
